package com.zxmr.utility.action;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.zxmr.action.Request;
import com.zxmr.action.Response;
import com.zxmr.action.ResponsePayload;
import com.zxmr.action.ResultCode;
import com.zxmr.domain.component.UserActionTrace;
import com.zxmr.navigate.ExecuteException;
import com.zxmr.navigate.Executor;
import com.zxmr.utility.dto.JSONListDTO;
import com.zxmr.utility.service.UserActionSearchBean;
import com.zxmr.utility.service.UserActionTraceService;

public class UserTraceAction extends Executor{

	enum method{
		add,
		query
	}
	@Override
	public void execute(Request request, Response response) {
		
		
		try
		{
			method m = Enum.valueOf(method.class, this.getElement().getMethod());
			switch(m)
			{
			case add:
				add(request,response);
				break;
			case query:
				queryAll(request,response);
				break;
			}

		}catch(Exception e)
		{
			ResponsePayload payload = new ResponsePayload();
			if(e instanceof ExecuteException)
			{
				payload.setValue(e.getMessage());
			}else
			{
				e.printStackTrace();
				payload.setValue("internal error");
			}
			response.setPayload(payload);
			response.setResult(ResultCode.COMPONENT_ERROR);
		}
	}
	private void add(Request request,Response response)throws ExecuteException
	{
		String para = request.getRequestParameters().get("parameters");
		UserActionTrace trace = JSON.parseObject(para, UserActionTrace.class);
		UserActionTraceService service = new UserActionTraceService();
		service.addActionTrace(trace);
	}
	private void queryAll(Request request,Response response)throws ExecuteException
	{
		UserActionSearchBean searchBean = setSearchBean(request);
		
		JSONListDTO list = query(searchBean);
		reply(response,list);
	}
	private JSONListDTO query(UserActionSearchBean searchBean)
	{
		UserActionTraceService service = new UserActionTraceService();
		
		List<UserActionTrace> opps = service.queryBySearchBean(searchBean);
		int total = service.queryTotalBySearchBean(searchBean);
		
		JSONListDTO list = new JSONListDTO();
		list.setTotal(total);
		list.setRows(opps);
		return list;
	}
	private UserActionSearchBean setSearchBean(Request request)
	{
		int count = getCountPerPage(request);
		int start = getCountStart(request);
		
		UserActionSearchBean searchBean = new UserActionSearchBean();
		searchBean.setCompany(request.getAccount().getCompany());
		searchBean.setLimit(count);
		searchBean.setStart(start);

		String employee = request.getRequestParameters().get("employee");
		String from = request.getRequestParameters().get("from");
		String to = request.getRequestParameters().get("to");

		
		searchBean.setUser(employee);
		searchBean.setFromTime(from);
		searchBean.setToTime(to);
		
		return searchBean;
	}
	private int getCountPerPage(Request request)
	{
		String count = request.getRequestParameters().get("size");
		if(count == null || "".equals(count.trim()))return 10;
		try
		{
			return Integer.parseInt(count);
		}catch(Exception e)
		{
			e.printStackTrace();
			return 10;
		}
	}
	private int getCountStart(Request request)
	{
		String start = request.getRequestParameters().get("start");
		if(start == null || "".equals(start.trim()))return 0;
		try
		{
			return Integer.parseInt(start);
		}catch(Exception e)
		{
			e.printStackTrace();
			return 0;
		}
	}
	private void reply(Response response,JSONListDTO dto)
	{
		ResponsePayload payload = new ResponsePayload(JSON.toJSONString(dto));
		response.setPayload(payload);
	}
}
